package com.cnhaimei.vod.logic;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.cnhaimei.com.widget.CircleFlowIndicator;
import com.cnhaimei.com.widget.ViewFlow;
import com.cnhaimei.vod.R;
import com.cnhaimei.vod.logic.FilmActivity.TableAdapter;
import com.cnhaimei.vod.net.dq.DQQuery;
import com.cnhaimei.vod.util.Const;

/**
 * 
 * @author funwell <br>
 * @date 2013-7-4
 *
 */
public class StarsActivity extends VODActivity implements OnGestureListener{
	private int mImageViewHeight = 184;
	private int mImageViewWidth = 185;
	
	private String mQueryTable = DQQuery.TABLE_STAR;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.v8_stars);
		initView();

		String queryTable = getIntent().getStringExtra(Const.Key.QUERY_TABLE);
		if (queryTable != null && queryTable.length() > 0) {
			mQueryTable = queryTable;
		}
	}

	//调整中间6个板块的位置
	private void updatePosition(View rootView)
	{
		DisplayMetrics dMetrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dMetrics);
		float xdpi = dMetrics.widthPixels;//dMetrics.xdpi;
		float ydpi = dMetrics.heightPixels;//dMetrics.ydpi;
		float fScaleX = xdpi / 480;//xdpi / 240;
		float fScaleY = ydpi / 800;//ydpi / 240;

		float fTopBottomScale = xdpi / 540;
		int nHeadHeight = (int)(98 * fTopBottomScale);//(int) (98 * fScaleY);
		int nFootHeight = (int)(116 * fTopBottomScale);//(int) (116 * fScaleY);	
		int nPanelHeight = dMetrics.heightPixels - nHeadHeight - nFootHeight;
		int nPaddingTop = (int)((nPanelHeight - (mImageViewHeight*3 - 22) * fScaleY) / 2);

		int nPaddingX = (int)((dMetrics.widthPixels - (mImageViewWidth + 20)*fScaleX*2)/4);
		if (rootView != null) {
			rootView.setPadding(nPaddingX, nPaddingTop, nPaddingX, 0);
		}
		updateImageViewPos(rootView, fScaleX, fScaleY, nPaddingX, R.id.gangtainv, R.id.gangtainan, R.id.dalunv, R.id.dalunan, R.id.guowainv, R.id.guowainan);		
		updateImageViewPos(rootView, fScaleX, fScaleY, nPaddingX, R.id.zuhe);		
	}

	protected void updateImageViewPos(View rootView, float fScaleX, float fScaleY, int paddingX, int... ids) {
		if (rootView == null) {
			return;
		}
		for (int i = 0; i < ids.length; i++) {
			View v = rootView.findViewById(ids[i]);
			if (v != null)
			{
				ViewGroup.LayoutParams lp = v.getLayoutParams();
				lp.height = (int)((int)mImageViewHeight * fScaleY);
				lp.width = (int) ((int)mImageViewWidth * fScaleX + paddingX*2);
				v.setLayoutParams(lp);
				((ImageView)v).setScaleType(ScaleType.FIT_XY);
				v.setPadding(paddingX, 0, paddingX, 0);
				
				v.setOnClickListener(this);
			}
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		super.onClick(v);
		int id = v.getId();
		String strQuery = "";
		String strTitle = "";	
		boolean bJump = true;
		switch (id) {
		case R.id.gangtainv:
			strQuery = DQQuery.QRY_STAR_GT_NV;
			strTitle = "港台女歌星";
			break;
		case R.id.gangtainan:
			strQuery = DQQuery.QRY_STAR_GT_NAN;
			strTitle = "港台男歌星";
			break;
		case R.id.dalunv:
			strQuery = DQQuery.QRY_STAR_DL_NV;
			strTitle = "大陆女歌星";
			break;
		case R.id.dalunan:
			strQuery = DQQuery.QRY_STAR_DL_NAN;
			strTitle = "大陆男歌星";
			break;
		case R.id.guowainv:
			strQuery = DQQuery.QRY_STAR_GW_NV;
			strTitle = "国外女歌星";
			break;
		case R.id.guowainan:
			strQuery = DQQuery.QRY_STAR_GW_NAN;
			strTitle = "国外男歌星";
			break;
		case R.id.zuhe:
			strQuery = DQQuery.QRY_STAR_ZH;
			strTitle = "组合";
			break;
		default:
			bJump = false;
			break;
		}
		if (bJump) {
			Intent intent = new Intent(Const.Action.showAllSingers);	
			intent.putExtra(Const.Key.QUERY_TABLE, mQueryTable);	
			intent.putExtra(Const.Key.QUERY_STRING, strQuery);
			intent.putExtra(Const.Key.TITLE_NAME, strTitle);
			StarsActivity.this.startActivity(intent);
		}
	}
	
	private OnTouchListener listener = new OnTouchListener() {
		
		public boolean onTouch(View v, MotionEvent event) {
			// TODO Auto-generated method stub
			StarsActivity.this.mDetector.onTouchEvent(event);
			return false;
		}
	};
	
	private GestureDetector mDetector;
	private ViewFlipper mHelpFlipper;
	private TextView	mPageText;
	private int mHelpFlipperIndex = 0;
	private int mHelpImgCount = 2;
	
	private ViewFlow mViewFlow;
	
	private void initView() {
//		mHelpFlipper = (ViewFlipper)findViewById(R.id.viewflipper);
//		mDetector = new GestureDetector(this);
//		mPageText = (TextView)findViewById(R.id.page_textview);

		showBackBtn(true);
		setTitle("歌星点歌");

		mViewFlow = (ViewFlow)findViewById(R.id.viewflow);
		mViewFlow.setAdapter(new TableAdapter(this));
        
        CircleFlowIndicator indic = (CircleFlowIndicator) findViewById(R.id.viewflowindic);
        mViewFlow.setFlowIndicator(indic);
        
//		setClickable( R.id.gangtainv, R.id.gangtainan, R.id.dalunv, R.id.dalunan, R.id.guowainv, R.id.guowainan, R.id.zuhe);		
//		updatePosition();
	}


	public class TableAdapter extends BaseAdapter {

		private LayoutInflater mInflater;
		private int[] ids = {R.layout.v8_stars_table1, R.layout.v8_stars_table2};

		public TableAdapter(Context context) {
			mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		@Override
		public int getCount() {
			return ids.length;   //返回很大的值使得getView中的position不断增大来实现循环。
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = mInflater.inflate(ids[position], null);
			}
			updatePosition(convertView);
			return convertView;
		}

	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (this.mDetector != null) {
			return this.mDetector.onTouchEvent(event);
		}
		
		return super.onTouchEvent(event);
	}

	public boolean onDown(MotionEvent arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean onFling(MotionEvent e1, MotionEvent e2, float arg2,
			float arg3) {
		Log.i("Fling", "Fling Happened!");
		if (e1.getX() - e2.getX() > 10) {
			this.mHelpFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,
					R.anim.push_left_out));
			this.mHelpFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
					R.anim.push_left_in));
			if (mHelpFlipperIndex < mHelpImgCount - 1) {
				this.mHelpFlipper.showNext();
				mHelpFlipperIndex++;
				//TODO:若有点号索引，则可在此处理
				
			}
			else {//达到最后一页，在翻页则退出。		
				this.mHelpFlipper.showPrevious();
				mHelpFlipperIndex--;
			}
			mPageText.setText((mHelpFlipperIndex + 1) + "/" + mHelpImgCount);
			return true;
		} else if (e1.getX() - e2.getX() < -10) {
			this.mHelpFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
					R.anim.push_right_in));
			this.mHelpFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,
					R.anim.push_right_out));
			if (mHelpFlipperIndex > 0) {
				this.mHelpFlipper.showPrevious();
				mHelpFlipperIndex--;
				//TODO:若有点号索引，则可在此处理
			}
			else
			{
				this.mHelpFlipper.showNext();
				mHelpFlipperIndex++;
			}
			mPageText.setText((mHelpFlipperIndex + 1) + "/" + mHelpImgCount);
			return true;
		}
		return true;
	}

	public void onLongPress(MotionEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2,
			float arg3) {
		// TODO Auto-generated method stub
		return false;
	}

	public void onShowPress(MotionEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public boolean onSingleTapUp(MotionEvent arg0) {
		// TODO Auto-generated method stub
		return false;
	}
}
